<?php

namespace common\models\dynamic;

use Yii;
use common\helpers\Url;
use common\helpers\StringHelper;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property string $province 省份
 * @property string $city 市
 * @property string $district 区
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class Dynamic extends \common\base\ActiveRecord implements \common\base\ShareDataInterface {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;
    use \common\traits\SoftDeleting;
    use \common\traits\QueryInstanceActiveRecord;

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    const STATUS_REFUSE = -1;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%dynamic}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['teacher_id', 'user_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['content'], 'string', 'max' => 1000],
            [['province', 'city', 'district'], 'string', 'max' => 6],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'teacher_id' => '教师ID',
            'user_id' => '用户ID',
            'content' => 'Content',
            'province' => '省份',
            'city' => '市',
            'district' => '区',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function getComments() {
        return $this->hasMany(DynamicComment::className(), ['dynamic_id' => 'id']);
    }

    public function getLikes() {
        return $this->hasMany(DynamicLike::className(), ['dynamic_id' => 'id']);
    }

    public function getGallery() {
        return $this->hasMany(DynamicGallery::className(), ['dynamic_id' => 'id']);
    }

    public function getLikeCount() {
        return $this->getLikes()->count();
    }

    public function getTopLikes() {
        if (empty($this->likes)) {
            return [];
        }
        $likes = [];
        $i = 0;
        foreach ($this->likes as $like) {
            if ($like->user) {
                $user = $like->user->toArray();
                $likes[] = $user['nickname'];
                $i++;
            }
            if ($i > 10) {
                break;
            }
        }
        return $likes;
    }

    public function afterFind() {
        if ($this->deleted_at) {
            $this->content = '抱歉已删除';
        }
        parent::afterFind();
    }

    public function getShareImage() {
        $image = '';
        $gallery = $this->gallery;
        if (!empty($gallery)) {
            $firstImage = array_shift($gallery);
            $image = Url::toRemote($firstImage->src);
        }

        return $image;
    }

    public static function findShareById($id) {
        $model = static::findOne($id);
        if (!$model) {
            return null;
        }
        return [
            'title' => StringHelper::truncate($model->content, 50),
            'desc' => StringHelper::truncate($model->content, 100),
            'img_url' => $model->shareImage,
            'link' => Url::share(['/dynamic/view', ['id' => $model->id]]),
        ];
    }

    /**
     * 教师基本信息
     * @return user
     */
    public function getUser() {
        return $this->hasOne(\common\models\user\UserMeta::className(), ['id' => 'user_id']);
    }

    public function getLiked() {
        return $this->hasOne(DynamicLike::className(), ['dynamic_id' => 'id'])->andWhere([
            'user_id' => Yii::$app->user->id,
        ])->exists();
    }

    public function getPics() {
        $gallery = $this->gallery;
        if (!empty($gallery)) {
            return ArrayHelper::getColumn($gallery, 'picture');
        }
        return [];
    }

    public function getThumbs() {
        $gallery = $this->gallery;
        if (!empty($gallery)) {
            return ArrayHelper::getColumn($gallery, 'thumbnail');
        }
        return [];
    }

    public function getSummaryComments() {
        $comments = [];
        if (!empty($this->comments)) {
            foreach ($this->comments as $comment) {
                $comments[] = [
                    'id' => $comment->id,
                    'username' => $comment->user->nickname,
                    'text' => $comment->content,
                ];
            }
        }
        return $comments;
    }

}
